package com.example.concurrentprogramming8016.thread.Atomic;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author liyanhao
 * @Date 2023-01-03 17:38
 */
public class AtomicIntegerTest {

    static AtomicInteger sum = new AtomicInteger(0);
    static Integer sum2 = 0;
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(() -> {
                for (int j = 0; j < 10000; j++) {
                    //原子递增 CAS
                    sum.incrementAndGet();
                    ++sum2;
                    //todo
                }
            });
            thread.start();
        }
        Thread.sleep(3000);
        //原子操作不会漏掉
        System.out.println(sum.get());
        //使用基本的 Integer 因为并发的原因 某些累加的操作会被漏掉
        System.out.println(sum2);
    }
}
